﻿using System.Globalization;
using System.Net;

namespace Wild2.Models.GeoIP
{
    public class Convertor
    {
        public static long ToInt(string addr)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(addr, "(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9])[.]){3}(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9]))"))
            {
                return (uint)IPAddress.NetworkToHostOrder((int)IPAddress.Parse(addr).Address);
            }
            return -1;
        }

        public static string ToAddr(long address)
        {
            return IPAddress.Parse(address.ToString(CultureInfo.InvariantCulture)).ToString();
        }
    }
}